﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Http;
using System.Web.Routing;
using Intelligent.OTC.Common.Utils;
using Intelligent.OTC.Common;
using System.Timers;
using Intelligent.OTC.Business;
using System.Collections;
using Intelligent.OTC.Domain.Partials;

namespace Intelligent.OTC.WebApi
{
    public class Global : System.Web.HttpApplication
    {
        public override void Init()
        {
            base.Init();
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
            //WebApiConfig.Register(GlobalConfiguration.Configuration);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            new SpringFactory();
            List<MailTimerWrapper> ts = SpringFactory.GetObjectImpl<List<MailTimerWrapper>>("MailTimer");
            foreach (var t in ts)
            {
                t.Elapsed += new ElapsedEventHandler(timer_Elapsed);
                if (t.Enabled)
                {
                    t.Start();
                }
            }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Helper.Log.Info("Start to process mail list.");
            (sender as MailTimerWrapper).Stop();
            try
            {
                SpringFactory.GetObjectImpl<MailService>("MailService").ProcessDealMailBoxs((sender as MailTimerWrapper).Deal);
            }
            catch (Exception ex)
            {
                Helper.Log.Error("ProcessDealMailBoxs failed.", ex);
            }
            finally
            {
                (sender as MailTimerWrapper).Start();
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_PostAuthorizeRequest()
        {
            if (HttpContext.Current.Request != null 
                && ((HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith("~/api"))
                || (HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith("~/odata"))))
                //|| HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith("~/User/LoginToken")))
            {
                bool isDev = false;
                if (bool.TryParse(System.Configuration.ConfigurationManager.AppSettings["IsDev"], out isDev) && isDev)
                {
                    HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
                    return;
                }

                if (HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.Contains("~/api/Collection"))
                {
                    HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.ReadOnly);
                }
                else
                {
                    HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.ReadOnly);
                }
            }
        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}